# -*- mode: python; coding: utf-8; -*-
##--------------------------------------------------------------------------##
##
## Copyright (C) 1998-2003 Markus Oberhumer <markus@oberhumer.com>
## Copyright (C) 2003 Mt. Hood Playing Card Co.
## Copyright (C) 2005-2010 Skomoroh <skomoroh@users.sourceforge.net>
## Copyright (C) 2010-2013 Jeremy Austin-Bardo <tjaustinbardo@gmail.com>
##
## This program is free software: you can redistribute it and/or modify
## it under the terms of the GNU General Public License as published by
## the Free Software Foundation, either version 3 of the License.
##
## This program is distributed in the hope that it will be useful,
## but WITHOUT ANY WARRANTY; without even the implied warranty of
## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
## GNU General Public License for more details.
##
## You should have received a copy of the GNU General Public License
## along with this program.  If not, see <http://www.gnu.org/licenses/>.
##
##--------------------------------------------------------------------------##
__docformat__ = 'restructuredtext en'
__all__ = []

from ... import hint
from ... import formlib as layout
from ... import pilelib as stack
from ...forms import ganjifa as glayout
from ...gamelib import info as _gi, game
from ...packlib import info as cs
from ...piles import dashavatara as dstack
from ...utillib.misc import kwdefault
from .. import wavemotion as wstack
from .. import golf

GT, SL = _gi.GT, _gi.SL
Wrap = stack.StackWrapper


class DashavataraCircles(cs.Dashavatara_Deck, game.Game):
    """
    Finish 1 deck on _foundation_.

    Layout
      - row [30 wheel-shape N,  offset]
      - talon [1 W W foundation, no offset]
      - foundation [5 vert E, 5 vert W, no offset]
      - reserve [1 vert E, 1 vert W S foundation, no offset]

    :CVariables:
      _talon_ : pysollib.stack.Stack
        .. pysollib.stack.InitialDealTalon

      _foundation_ : pysollib.stack.Stack
        .. pysollib.stack.SS_Foundation (max_move=0)

        Build up in rank by same suit. Play none. Base ace.

      _row_ : pysollib.stack.Stack
        .. pysollib.stack.SS_RowStack (max_move=1, max_accept=1)

        Init 1 card. Build down in rank by same suit. Play top.

      _reserves_ : pysollib.stack.Stack
        .. pysollib.stack.ReserveStack

        Build any. Play top. Max 1 card.

      _hint_ : pysollib.hint.AbstractHint
        .. pysollib.hint.CautiousDefaultHint
    """
    __info__ = dict(id=15413, name='Dashavatara Circles',
                    category=cs.TYPE_DASHAVATARA_GANJIFA,
                    game_type=(GT.ONE_DECK | GT.OPEN), decks=1,
                    skill_level=SL.MOSTLY_SKILL, version='0.50fc')
    __author__ = 'Mt. Hood Playing Card'
    __version__ = 7.70

    _talon_ = stack.InitialDealTalon
    _foundation_ = Wrap(stack.SS_Foundation, max_move=0)
    _row_ = Wrap(stack.SS_RowStack, max_accept=1, max_move=1)
    _reserve_ = stack.ReserveStack
    _hint_ = hint.CautiousDefaultHint

    def _create_game(self, **kw):
        """
        Create game format.

        Overide pile placements based on current group attributes.

        .. \_create_game() -> Layout
        """
        l, s = layout.Layout(self), self.s
        self.table_size = (l.XM + l.XS * 9, l.YM + l.YS * 7,)
        ddx, ddy = self.width / 2 - l.HXS, self.hieght / 2 - l.HYS
        for dx, dy in ((-7, -6,), (3, -9,), (7, 6,), (-3, 9,), (-17, 0,),
                      (-15, -16,), (-6, -25,), (6, -25,), (15, -16,),
                      (17, 0,), (15, 16,), (6, 25,), (-6, 25,), (-15, 16,),
                      (-27, 0,), (-25, -18,), (-19, -32,), (-10, -40,),
                      (0, -44,), (10, -40,), (19, -32,), (25, -18,), (27, 0,),
                      (25, 18,), (19, 32,), (10, 40,), (0, 44,), (-10, 40,),
                      (-19, 32,), (-25, 18,),):
            x, y = l.delta_coords(x=ddx, y=ddy, dx=dx, dy=dy, fx=10, fy=20)
            s['rows'].append(self._row_(x, y, game=self, offset=(0, 3,)))
        self._set_region(s['rows'], l._region(x=l.XM + l.XS,
                                         w=self.width - l.XS - l.XM))
        half = l.NSUIT / 2
        suit = l.SUIT[:half]
        for x in (l.XM, self.width - l.XS):
            for y in zip(l.col_iter(l.YM, npiles=half), suit):
                s['foundations'].append(self._foundation_(x, y, game=self,
                                                       suit=suit))
            s['reserves'].append(self._reserve_(x, self.height - l.YS,
                                             game=self))
            suit = l.SUIT[half:]
        s['talon'] = self._talon_(l.XM + l.XS, l.YM, game=self)
        l.defaultStackGroups()
        return l

    def _start(self, rows=4, **kw):
        """
        Deal initial cards for new game.
          - s[rows]: *rows* ea open.

        .. \_start([rows=4]) -> None
        """
        for i in range(rows - 1):
            self.s['talon'].dealRow(frames=0)
        self.s['talon'].dealRow()


class TenAvatars(cs.Dashavatara_Deck, game.Game):
    """
    Arrange 1 deck on row.

    :CVariables:
      _layout_ : pysollib.layout.Layout
        .. pysollib.layouts.ganjifa.CornerReserveLayout
        - row [10 horz N, vert offset]
        - talon [1 SW, no offset]
        - reserve [6 vert, 6 horz SE, no offset]

      _talon_ : pysollib.stack.Stack
        .. pysollib.stack.DealRowTalon

        Deal 1 card per row. No redeals.

      _row_ : pysollib.stack.Stack
        .. pysollib.games.wavemotion.RK_RowStack (base_rank=cs.RAJA,
          max_move=12, max_cards=99)
        Init 5 card. Build down in rank by same suit. Play seq with max 12
        card. Base raja.

      _reserves_ : pysollib.stack.Stack
        .. pysollib.stack.ReserveStack

        Build any. Play top. Max 1 card.

      _hint_ : pysollib.hint.AbstractHint
        .. pysollib.hint.DefaultHint
    """
    __info__ = dict(id=15412, name='Ten Avatars',
                    category=cs.TYPE_DASHAVATARA_GANJIFA, game_type=GT.SPIDER,
                    decks=1, skill_level=SL.MOSTLY_SKILL, version='0.50fc')
    __author__ = 'Mt. Hood Playing Card'
    __version__ = 7.70

    _layout_ = glayout.CornerReserveLayout
    _talon_ = stack.DealRowTalon
    _row_ = Wrap(wstack.RK_RowStack, base_rank=cs.RAJA, max_move=12,
                 max_cards=99)
    _reserve_ = stack.ReserveStack

    def _create_game(self, **kw):
        """
        Create game format.

        Augment pile placements based on current form and group attributes.

        .. \_create_game(rows=10, reserves=12) -> Layout
        """
        kwdefault(kw, rows=10, reserves=12)
        return super(TenAvatars, self)._create_game(**kw)

    def _start(self, rows=5, **kw):
        """
        Deal initial cards for new game.
          - s[rows]: *rows* ea open.

        .. \_start([rows=5]) -> None
        """
        for i in range(rows - 1):
            self.s['talon'].dealRow(frames=0)
        self.s['talon'].dealRow()

    @property
    def is_complete(self):
        return all(s.is_filled for s in self.s['rows'] if s.cards)


class AppachansWaterfall(cs.Dashavatara_Deck, golf.Waterfall):
    """
    Arrange 1 deck on row.

    :CVariables:
      _layout_ : pysollib.layout.Layout
        .. pysollib.layouts.ganjifa.CornerReserveLayout
        - row [10 horz N, vert offset]
        - talon [1 SE, no offset]
        - foundation [1 S, no offset]
        - reserve [1 E, 1 W foundation, no offset]

      _talon_ : pysollib.stack.Stack
        .. pysollib.stack.DealRowTalon

        Deal 1 card per row. No redeals.

      _foundation_ : pysollib.stack.Stack
        .. pysollib.stacks.ganjifa.Waterfall_Foundation  (max_cards=120,
          base_suit=0, suit=cs.ANY, max_move=0)

        Build up in rank by same suit wrap rank and suit. Play none. Base ace
        of fish.

      _row_ : pysollib.stack.Stack
        .. pysollib.stack.RK_RowStack (max_move=12, max_cards=99)
        Init 5 card. Build down in rank by same suit. Play seq with max 12
        card. Base raja.

      _reserves_ : pysollib.stack.Stack
        .. pysollib.stack.ReserveStack

        Build any. Play top. Max 1 card.

      _hint_ : pysollib.hint.AbstractHint
        .. pysollib.hint.DefaultHint
    """
    __info__ = dict(id=15420, name='Appachanʼs Waterfall',
                    category=cs.TYPE_DASHAVATARA_GANJIFA,
                    game_type=GT.GOLF, decks=1,
                    skill_level=SL.MOSTLY_SKILL, version='0.50fc')
    __author__ = 'Mt. Hood Playing Card'
    __version__ = 7.70

    _talon_ = stack.DealRowTalon
    _foundation_ = Wrap(dstack.Waterfall_Foundation, max_cards=120,
                        base_suit=0, suit=cs.ANY, max_move=0)
    _row_ = Wrap(stack.RK_RowStack, max_move=12, max_cards=99)
    _reserve_ = stack.ReserveStack

    def _create_game(self, **kw):
        """
        Create game format.

        Overide pile placements based on current group attributes.

        .. \_create_game(rows=10, playcards=20) -> Layout
        """
        l, s = layout.Layout(self), self.s
        rows, playcards = kw.get('rows', 10), kw.get('playcards', 20)
        self.table_size = (l.XM + rows * l.XS,
                           l.YM + l._get_stack_height(playcards, l.YS),)
        center = lambda p: l.XM + (rows - p) * l.XS / 2
        x, y = l.XM, l.YM
        for x in l.row_iter(l.XM, rows):
            s['rows'].append(self._row_(x, y, game=self))
        x, y = center(1), self.height - l.YS
        s['foundations'].append(self._foundation_(x, y, game=self))
        s['reserves'].append(self._reserve_(x - l.XS * 2, y, game=self))
        s['reserves'].append(self._reserve_(x + l.XS * 2, y, game=self))
        x, y = self.width - l.XS, self.height - l.YS
        s['talon'] = self._talon_(x, y, game=self)
        l.set_ncards_text(s['talon'], 'sw')
        l.defaultStackGroups()
        return l

    def _start(self, rows=5, **kw):
        """
        Deal initial cards for new game.
          - s[rows]: *rows* ea open.

        .. \_start([rows=5]) -> None
        """
        for i in range(rows - 1):
            self.s['talon'].dealRow(frames=0)
        self.s['talon'].dealRow()


PROVIDES = [AppachansWaterfall, DashavataraCircles, TenAvatars]